#!/bin/bash

#  NOTE(koekeishiya): A list of all built-in modifier and literal keywords can
#                     be found at https://github.com/koekeishiya/skhd/issues/1
#
#                     A hotkey is written according to the following rules:
#
#                       hotkey       = <mode> '<' <action> | <action>
#
#                       mode         = 'name of mode' | <mode> ',' <mode>
#
#                       action       = <keysym> '[' <proc_map_lst> ']' | <keysym> '->' '[' <proc_map_lst> ']'
#                                      <keysym> ':' <command>          | <keysym> '->' ':' <command>
#                                      <keysym> ';' <mode>             | <keysym> '->' ';' <mode>
#
#                       keysym       = <mod> '-' <key> | <key>
#
#                       mod          = 'modifier keyword' | <mod> '+' <mod>
#
#                       key          = <literal> | <keycode>
#
#                       literal      = 'single letter or built-in keyword'
#
#                       keycode      = 'apple keyboard kVK_<Key> values (0x3C)'
#
#                       proc_map_lst = * <proc_map>
#
#                       proc_map     = <string> ':' <command>
#
#                       string       = '"' 'sequence of characters' '"'
#
#                       command      = command is executed through '$SHELL -c' and
#                                      follows valid shell syntax. if the $SHELL environment
#                                      variable is not set, it will default to '/bin/bash'.
#                                      when bash is used, the ';' delimeter can be specified
#                                      to chain commands.
#
#                                      to allow a command to extend into multiple lines,
#                                      prepend '\' at the end of the previous line.
#
#                                      an EOL character signifies the end of the bind.
#
#                       ->           = keypress is not consumed by skhd
#
#  NOTE(koekeishiya): A mode is declared according to the following rules:
#
#                       mode_decl = '::' <name> '@' ':' <command> | '::' <name> ':' <command> |
#                                   '::' <name> '@'               | '::' <name>
#
#                       name      = desired name for this mode,
#
#                       @         = capture keypresses regardless of being bound to an action
#
#                       command   = command is executed through '$SHELL -c' and
#                                   follows valid shell syntax. if the $SHELL environment
#                                   variable is not set, it will default to '/bin/bash'.
#                                   when bash is used, the ';' delimeter can be specified
#                                   to chain commands.
#
#                                   to allow a command to extend into multiple lines,
#                                   prepend '\' at the end of the previous line.
#
#                                   an EOL character signifies the end of the bind.

# add an on_enter command to the default mode
# :: default : chunkc border::color 0xff775759
#
# defines a new mode 'test' with an on_enter command, that captures keypresses
# :: test @ : chunkc border::color 0xff24ccaa
#
# from 'default' mode, activate mode 'test'
# cmd - x ; test
#
# from 'test' mode, activate mode 'default'
# test < cmd - x ; default
#
# launch a new terminal instance when in either 'default' or 'test' mode
# default, test < cmd - return : open -na /Applications/Terminal.app

# application specific bindings
#
# cmd - n [
#     "kitty"       : echo "hello kitty"
#     "qutebrowser" : echo "hello qutebrowser"
#     "finder"      : false
# ]

# narze's config
# Legends
# Leader : Alt
# Navigation : H / J / K / L (vim)
# Prew / Next : < / >
# Move : Leader + Shift
# Resize : Leader + Cmd
# Monitor : I / O
# Desktop : U / P or Leader + Ctrl

# Keycodes
# 0x23 = Colemak semicolon / Qwerty P
# 0x2B = Comma / <
# 0x2F = Fullstop / >
# 0x2C = Slash
# open terminal
# cmd - return : /Applications/kitty.app/Contents/MacOS/kitty --single-instance -d ~ &> /dev/null

# close focused window
# alt - w : yabai -m window --close

# focus window
alt - j : yabai -m window --focus south
alt - h : yabai -m window --focus west
alt - k : yabai -m window --focus north
alt - l : yabai -m window --focus east
# alt - m : yabai -m window --focus recent

# focus window in stack
alt - 0x2B : yabai -m window --focus stack.prev
alt - 0x2F : yabai -m window --focus stack.next
alt - 0x2C : yabai -m window --focus stack.recent

# focus monitor
alt - u  : ~/.yabai_focus_display prev
# alt + ctrl - h  : ~/.yabai_focus_display prev
alt - y : ~/.yabai_focus_display next
# alt + ctrl - i  : ~/.yabai_focus_display next

# move window
alt + cmd + shift - j : yabai -m window --warp south
alt + cmd + shift - h : yabai -m window --warp west
alt + cmd + shift - k : yabai -m window --warp north
alt + cmd + shift - l : yabai -m window --warp east

# swap window
alt + shift - j : yabai -m window --swap south
alt + shift - h : yabai -m window --swap west
alt + shift - k : yabai -m window --swap north
alt + shift - l : yabai -m window --swap east

# stack window
# alt + ctrl + shift - h  : yabai -m window --stack west
# alt + ctrl + shift - j  : yabai -m window --stack south
# alt + ctrl + shift - k  : yabai -m window --stack north
# alt + ctrl + shift - l  : yabai -m window --stack east

# make floating window fill screen, make it float if not already floating
ctrl + alt + cmd - t : yabai -m window --toggle float; \
                       floating=$(yabai -m query --windows --window | jq .floating); \
                       [ $floating -eq 1 ] && yabai -m window --grid 1:1:0:0:1:1

# float fill left half
ctrl + alt + cmd - h : yabai -m window --grid 1:2:0:0:1:1
ctrl + alt + cmd - m : yabai -m window --grid 1:2:0:0:1:1

# float fill right half
ctrl + alt + cmd - i : yabai -m window --grid 1:2:1:0:1:1

# float fill bottom half
ctrl + alt + cmd - n : yabai -m window --grid 2:1:0:1:1:1

# float fill top half
ctrl + alt + cmd - e : yabai -m window --grid 2:1:0:0:1:1


# create desktop
ctrl + cmd + alt - m : yabai -m space --create;

# create desktop, move window and follow focus
ctrl + alt + cmd - k : yabai -m space --create; \
                       yabai -m window --space last; \
                       yabai -m space --focus last; \
                       yabai -m window --grid 1:1:0:0:1:1

# destroy desktop
ctrl + cmd + alt + shift - w : yabai -m space --focus prev; \
                               yabai -m space next --destroy

# fast focus desktop
# alt + ctrl - 0x2B : yabai -m space --focus recent
# alt - l : yabai -m space --focus prev
# alt + ctrl - h : yabai -m space --focus prev
# alt - 0x23 : yabai -m space --focus next
# alt + ctrl - i : yabai -m space --focus next
alt - 1 : yabai -m space --focus 1
alt - 2 : yabai -m space --focus 2
alt - 3 : yabai -m space --focus 3
alt - 4 : yabai -m space --focus 4

# send window to desktop
# alt + ctrl + shift - 0x2B : yabai -m window --space recent
# alt + shift - l : yabai -m window --space prev
# alt + ctrl + shift - h  : yabai -m window --space prev
# alt + shift - 0x23 : yabai -m window --space next
shift + alt - 1 : yabai -m window --space 1
shift + alt - 2 : yabai -m window --space 2
shift + alt - 3 : yabai -m window --space 3
shift + alt - 4 : yabai -m window --space 4
shift + alt + cmd - 1 : yabai -m window --space 1; yabai -m space --focus 1
shift + alt + cmd - 2 : yabai -m window --space 2; yabai -m space --focus 2
shift + alt + cmd - 3 : yabai -m window --space 3; yabai -m space --focus 3
shift + alt + cmd - 4 : yabai -m window --space 4; yabai -m space --focus 4

# send window to desktop and follow focus
# shift + cmd - x : yabai -m window --space $(chunkc get _last_active_desktop); yabai -m space --focus $(chunkc get _last_active_desktop)
# shift + cmd - z : yabai -m window --space prev; yabai -m space --focus prev
# shift + cmd - c : yabai -m window --space next; yabai -m space --focus next
# shift + cmd - 1 : yabai -m window --space 1; yabai -m space --focus 1
# shift + cmd - 2 : yabai -m window --space 2; yabai -m space --focus 2
# shift + cmd - 3 : yabai -m window --space 3; yabai -m space --focus 3
# shift + cmd - 4 : yabai -m window --space 4; yabai -m space --focus 4

ctrl + alt - 1  : yabai -m display --focus 1
ctrl + alt - 2  : yabai -m display --focus 2
ctrl + alt - 3  : yabai -m display --focus 3

# send window to monitor and follow focus
alt + shift - u : yabai -m window --display prev; yabai -m display --focus prev
alt + shift - y : yabai -m window --display next; yabai -m display --focus next
# shift + alt + cmd - 1  : yabai -m window --display 1; yabai -m display --focus 1
# shift + alt + cmd - 2  : yabai -m window --display 2; yabai -m display --focus 2
# shift + alt + cmd - 3  : yabai -m window --display 3; yabai -m display --focus 3

# increase region size
alt + cmd - a : yabai -m window --resize left:-80:0
alt + cmd - r : yabai -m window --resize bottom:0:80
alt + cmd - w : yabai -m window --resize top:0:-80
alt + cmd - s : yabai -m window --resize right:80:0

# decrease region size
# alt + cmd + ctrl - a : yabai -m window --resize left:80:0
# alt + cmd + ctrl - r : yabai -m window --resize bottom:0:-80
# alt + cmd + ctrl - w : yabai -m window --resize top:0:80
# alt + cmd + ctrl - s : yabai -m window --resize right:-80:0

# set insertion point for focused container
ctrl + alt - h : yabai -m window --insert west
ctrl + alt - j : yabai -m window --insert south
ctrl + alt - k : yabai -m window --insert north
ctrl + alt - l : yabai -m window --insert east

# rotate tree
# alt - r : yabai -m space --rotate 90

# mirror tree y-axis
# alt + shift - x : yabai -m space --mirror vertical

# mirror tree x-axis
# alt - x : yabai -m space --mirror horizontal

# toggle desktop padding
# alt - a : yabai -m space --toggle padding

# toggle window fullscreen
# alt - f : yabai -m window --toggle fullscreen

# toggle window native fullscreen
# shift + alt - f : yabai -m window --toggle native-fullscreen

# toggle window zoom
alt - z : yabai -m window --toggle zoom-fullscreen

# toggle window split type
alt - c : yabai -m window --toggle split

# toggle window fade
# alt - q : yabai -m window --toggle fade

# float / unfloat window and center on screen
alt - t : yabai -m window --toggle float;\
          yabai -m window --grid 6:6:1:1:4:4

# toggle sticky
# alt - s : yabai -m window --toggle sticky

# toggle border
# alt - b : yabai -m window --toggle border

# classic pip
# alt - p : yabai -m window --toggle float;\
#           yabai -m window --toggle sticky;\
#           yabai -m window --toggle topmost;\
#           yabai -m window --grid 5:5:3:0:2:2

# toggle sticky, float and resize to picture-in-picture size
# alt + shift - p : yabai -m window --toggle sticky;\
#                  yabai -m window --toggle topmost;\
#                  yabai -m window --toggle border;\
#                  yabai -m window --toggle pip

# float next window to be tiled
# shift + alt - t : chunkc set window_float_next 1


# ctrl + alt - l : yabai -m space --deserialize ~/.chunkwm_layout
# ctrl + alt + cmd - l : yabai -m space --serialize ~/.chunkwm_layout

# Fix screenshot taking in app mode
cmd + shift - 4 -> : yabai -m window --toggle border; sleep 3; yabai -m window --toggle border;
ctrl + cmd + shift - 4 -> : yabai -m window --toggle border; sleep 3; yabai -m window --toggle border;

# MPC commands
# ctrl + alt + cmd + shift - 0x23 : mpc toggle
# ctrl + alt + cmd - 0x2B : mpc prev
# ctrl + alt + cmd - 0x2F : mpc next

# Reload yabai
ctrl + alt + cmd - z : osascript -e 'tell application "Keyboard Maestro Engine" to do script "Hide All Windows"' && \
                       launchctl kickstart -k "gui/${UID}/homebrew.mxcl.yabai"

# Float everything in screen except current window
ctrl + alt + cmd - x : ids=$(yabai -m query --windows --space | jq -r ".[] | select(.floating == 0) | .id") && \
                        while IFS= read -r id; do \
                          yabai -m window $id --toggle float; \
                        done <<< "$ids" && \
                        ids=$(yabai -m query --windows --space | jq -r ".[] | select(.floating == 1) | .id") && \
                        while IFS= read -r id; do \
                          yabai -m window $id --grid 6:6:1:1:4:4; \
                        done <<< "$ids" && \

# Unfloat
ctrl + alt + cmd - c : ids=$(yabai -m query --windows --space | jq -r ".[] | select(.floating == 1) | .id") && \
                        while IFS= read -r id; do \
                          yabai -m window $id --toggle float ; \
                        done <<< "$ids"

# Balance windows
alt - 0x2C : yabai -m space --balance

# Toggle space windows gap
# alt - d : yabai -m space --toggle gap

# Toggle space windows padding
# alt - g : yabai -m space --toggle padding

# Task randomizer
# ctrl + alt + cmd + shift - d : osascript ~/Library/Scripts/omnifocus-task-randomizer.applescript

# Hyper 1-0
# ctrl + alt + cmd + shift - 1 : /Applications/Microsoft\ Edge.app/Contents/MacOS/Microsoft\ Edge

# change layout of desktop
ctrl + alt - a : yabai -m space --layout bsp
ctrl + alt - d : yabai -m space --layout float
ctrl + alt - s : yabai -m space --layout $(yabai -m query --spaces --space | jq -r 'if .type == "bsp" then "float" else "bsp" end')
